﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_ControlChuyenNguoiBaoTro : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag1"] == null)
            {
                ViewState["flag1"] = false;
            }
            if (ViewState["flag2"] == null)
            {
                ViewState["flag2"] = false;
            }

            LoadChiNhanh();
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        txtMaNPP.Text = "";
        txtNameNPP.Text = "";
        txtMaBaoTroTrucTiep.Text = "";
        txtNameBaoTroTrucTiep.Text = "";
        txtMaBaoTroMoi.Text = "";
        txtNameBaoTroMoi.Text = "";
    }
    protected void ImageBtnCheck_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (((bool)ViewState["flag1"]) && ((bool)ViewState["flag2"]))
            {
                bool kq=dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_changeRecommender", ref error,
                    new SqlParameter("@Salesp_key",txtMaNPP.Text),
                    new SqlParameter("@Salesp_keyTuyenTrenCu",txtMaBaoTroTrucTiep.Text),
                    new SqlParameter("@NewSalesp_key",txtMaBaoTroMoi.Text),
                    new SqlParameter("@MoveAll", chbChuyenToanBo.Checked),
                    new SqlParameter("@userID",Session["user"].ToString()));
                if (kq)
                {
                    uscMsgBox1.AddMessage("Chuyển người bảo trợ thành công", uscMsgBox.enmMessageType.Error);
                }
                else
                {
                    uscMsgBox1.AddMessage("Hệ thống đang bảo trì", uscMsgBox.enmMessageType.Error);
                }
            }
            else
            {
                uscMsgBox1.AddMessage("Thông tin không đầy đủ để chuyển người bảo trợ", uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void LoadChiNhanh()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getDistributorList]", CommandType.StoredProcedure);
            dt.BindDropDownList(db, ddlChiNhanh, "Sitecode", "sitename");
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void txtMaNPP_TextChanged(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getDetailNPPBaoTro", CommandType.StoredProcedure,
                new SqlParameter("@salesp_key", txtMaNPP.Text),
                new SqlParameter("@Sitecode",ddlChiNhanh.SelectedValue.ToString()));
            if (db.Rows.Count > 0)
            {
                txtNameNPP.Text = db.Rows[0]["NPP"].ToString(); 
                txtMaBaoTroTrucTiep.Text = db.Rows[0]["recommender"].ToString();
                txtNameBaoTroTrucTiep.Text = db.Rows[0]["BaoTro"].ToString();
                
            }
            else
            {
                uscMsgBox1.AddMessage("Mã nhà phân phối không chính xác", uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void txtMaBaoTroMoi_TextChanged(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            txtNameBaoTroMoi.Text = dt.GetDataDepend(str, "k_getSalesp_nameBySitecode", CommandType.StoredProcedure, ref error,
                new SqlParameter("@salesp_key", txtMaBaoTroMoi.Text),
                new SqlParameter("@Sitecode",ddlChiNhanh.SelectedValue.ToString()));
           
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }

}